<?php

/**
 * Class Client
 */
namespace app\common\request;

class ClientLai{

    protected $user_name = '';                  //用户名
    protected $ip = '';                          //请求的ip
    protected $time_stamp = 0;                  //请求的毫秒时间戳
    protected $key = '';                        //系统id对应的密钥
    protected $sign = '';                       //签名
    protected $seq_id = '';                     //请求id（唯一值）
    protected $http_request_headers = array();//请求头

    /**
     * @function    构造方法
     * @param null
     * @return object
     * @user    allen
     * @date    2016/10/18   11:43
     */
    public function __construct($user_name,$key)
    {
        $this->init($user_name,$key);
    }

    /**
     * @function 初始化
     * @param $user_name
     * @param $key
     * @return null
     * @user    allen
     * @date    2016/10/18   11:43
     */
    public function init($user_name,$key)
    {
        $this->ip                           = '120.79.31.177';
        $this->user_name                   = $user_name;
        $this->key                          = $key;
        $this->time_stamp                  = time()*1000;
        // $this->guid                         = create_guid();
        $this->seq_id                       = $this->create_seq_id();
        $this->sign                         = $this->create_sign();
    }

    /**
     * @function    创建请求id（不可重复）
     * @param null
     * @return string
     * @user    allen
     * @date    2016/10/18   11:43
     */
    public function create_seq_id()
    {
        $charid = strtoupper(md5(uniqid(mt_rand(), true)));
        $hyphen = chr(45);// "-"
        $uuid = chr(123)// "{"
            .substr($charid, 0, 8).$hyphen
            .substr($charid, 8, 4).$hyphen
            .substr($charid,12, 4).$hyphen
            .substr($charid,16, 4).$hyphen
            .substr($charid,20,12)
            .chr(125);// "}"
        $uuid = str_replace(array('{','}','-'),array('','',''),$uuid);
        return $uuid;
    }

    /**
     * @function 生成签名
     * @param
     * @return null
     * @author  allen
     * @date    2016/10/18   11:43
     */
    public function create_sign()
    {
        $string = '';                                               //第一步：对参数按照key=value的格式，并按照参数名ASCII字典序排序如下
        $string.= "user_name=".$this->user_name;
        $string.= "&ip=".$this->ip;
        $string.= "&time_stamp=".$this->time_stamp;
        $string.= "&seq_id=".$this->seq_id;
        $string.= "&key=".$this->key;                             //第二步：拼接API密钥
        return strtoupper(md5($string));                           //第三部：md5加密，并转化为大写
    }

    /**
     * @function    创建请求头信息
     * @param $system_name
     * @author  allen
     * @date    2016/10/18   11:43
     */
    public function create_http_request_headers()
    {
        $headers = array();
        $headers[] = 'User-Name:'.$this->user_name;
        $headers[] = 'Ip:'.$this->ip;
        $headers[] = 'Time-Stamp:'.$this->time_stamp;
        $headers[] = 'Seq-Id:'.$this->seq_id;
        $headers[] = 'Sign:'.$this->sign;
        $this->http_request_headers = $headers;
    }

    /**
     * @function get请求
     * @author 紫梦
     * @date    2018-02-23
     */
    public function request_get($url)
    {
        $this->create_http_request_headers();
        $curl = new Curl; 
        $res = $curl->set('CURLOPT_HTTPHEADER',$this->http_request_headers)->url($url);
        // 任务结果状态
        if ($curl->error()) {
            $arr['result'] = 1;
            $arr['msg'] = $curl->message();
            $content = json_encode($arr);
        } else {
            // 任务进程信息
            $info = $curl->info();
            
            if($info['http_code'] == 500){
                $arr['result'] = 1;
    
                $arr['msg'] = '对方服务器错误';
                $content = json_encode($arr);

            }else{

                // 任务结果内容
                $content = $curl->data();
            }
        }
        return $content;

    }

    /**
     * @function post 请求
     * @params  null
     * @return null
     * @author allen
     * @date    2016/10/18   11:43
     */
    public function request_post($url,$post_data)
    {
        $this->create_http_request_headers();
        $res = self::curl_post($url,$post_data ,$this->http_request_headers);
        if($res)
        {
            return $res;
        }
        else
        {
            return ['result'=>-32,'msg'=>'请求失败(post)'];
        }
    }

    public static function curl_post($url='https://zi-m.cn',$data,$header)
    {
        $curl = new Curl; 
        // dump($curl);
        $res = $curl->set('CURLOPT_HTTPHEADER',$header)->post($data)->url($url);
        // 任务结果状态
        if ($curl->error()) {
            $arr['result'] = 1;
            $arr['msg'] = $curl->message();
            $content = json_encode($arr);
        } else {
            // 任务进程信息
            $info = $curl->info();
            
            if($info['http_code'] == 500){
                $arr['result'] = 1;
    
                $arr['msg'] = '对方服务器错误';
                $content = json_encode($arr);

            }else{

                // 任务结果内容
                $content = $curl->data();
            }
        }


        return $content;
    }


}
